package com.qf.shop.timer;

import com.qf.shop.entity.Information;
import com.qf.shop.service.IInformationService;
import com.qf.shop.utils.JPushUtils;
import com.qf.shop.vo.MaxTime;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Transactional
public class UpdateInformation {


    private  IInformationService iInformationService;



    public UpdateInformation(IInformationService iInformationService) {
        this.iInformationService = iInformationService;
    }

    public void updateInformation(){


        MaxTime maxTime = iInformationService.getMaxTime();
        Date maxTime1 = maxTime.getMaxTime();
        long time = maxTime1.getTime();
        Date date1 = new Date();
        long time1 = date1.getTime();

        List<Information> updateInformation1=iInformationService.getUpdateInformation();

        for (Information information : updateInformation1) {
            Date createTime = information.getCreateTime();
            if (time1 >= createTime.getTime()){
                //推送
                Map<String, String> param = new HashMap<>();
                param.put("msg", information.getMessage());
                param.put("title",information.getTitle());
                JPushUtils.jpushAndroid(param);
            }
        }


        if (time1>=time){

            List<Information> updateInformation = iInformationService.getUpdateInformation();
            for (Information information : updateInformation) {
                information.setStatus("1");
            }
            for (Information information : updateInformation) {
                iInformationService.aChangeInformation(information);
            }
//            iInformationService.againChangeInformation(updateInformation);

        }


    }
}
